package com.xebia.lightning.converter;

import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DBObject;

@Component
public class LocationSkillsCountVOReadConverter implements org.springframework.core.convert.converter.Converter<DBObject, LocationSkillsCountVO> {

    @Override
    public LocationSkillsCountVO convert(DBObject source) {
        LocationSkillsCountVO vo = new LocationSkillsCountVO();
        CommandResult result = (CommandResult) source.get("_id");
        String location = result.getString("location");
        String skill = result.getString("skill");
        vo.setLocation(location);
        vo.setSkill(skill);
        Object object = source.get("value");
        if (object instanceof BasicDBObject) {
            BasicDBObject dbObject = (BasicDBObject) object;
            Object countObj = dbObject.get("count");
            if (countObj == null) {
                countObj = dbObject.get("key");
            }
            vo.setCount(((Double) countObj).longValue());
        } else if (object instanceof Double) {
            vo.setCount(((Double) object).longValue());
        }
        return vo;
    }

}
